'use strict'; const { Document } = require('/document'); const { ShapeNodeDefinition } = require('/nodes'); const { ShapeRectangle } = require('/shapes'); const { Colour } = require('/colours'); const { FillDescriptor } = require('/fills'); const { AddChildNodesCommandBuilder } = require('/commands'); const { Dialog, DialogResult, UnitType } = require('/dialog.js'); const doc = Document.current; function rand(min, max) { return Math.random() * (max - min) + min; } function isOk(result) { return (result?.value ?? result) === DialogResult.Ok.value; } if (!doc) { console.log('ERROR: No document open'); } else { const dlg = Dialog.create('Create Rectangles'); const col = dlg.addColumn(); const g1 = col.addGroup(''); const shapeCtrl = g1.addUnitValueEditor('Number of shapes', UnitType.Number, UnitType.Number, 200, 1, 2000).setPrecision(0); const startxCtrl = g1.addUnitValueEditor('Start (x)', UnitType.Number, UnitType.Number, 1, -300, 2000).setPrecision(2); const startyCtrl = g1.addUnitValueEditor('Start (y)', UnitType.Number, UnitType.Number, 1, -300, 2000).setPrecision(2); const widthCtrl = g1.addUnitValueEditor('Width (x)', UnitType.Number, UnitType.Number, 3000, 1, 5000).setPrecision(2); const heightCtrl = g1.addUnitValueEditor('Height (y)', UnitType.Number, UnitType.Number, 2000, 1, 5000).setPrecision(2); const randXCtrl = g1.addUnitValueEditor('Shape (x)', UnitType.Number, UnitType.Number, 300, 1, 5000).setPrecision(2); const randYCtrl = g1.addUnitValueEditor('Shape (y)', UnitType.Number, UnitType.Number, 300, 1, 5000).setPrecision(2); const linkCtrl = g1.addSwitch("Link shape xy", false) const randomCtrl = g1.addSwitch("Random shape size", true) dlg.initialWidth = 380; const result = dlg.runModal(); if (isOk(result)) { const count = shapeCtrl.value; // Spread shapes across a generous canvas area const canvasW = widthCtrl.value; const canvasH = heightCtrl.value; const builder = AddChildNodesCommandBuilder.create(); for (let i = 0; i < count; i++) { // Random size: min 30px let w = 30; let h=30; if (randomCtrl.value) { w = rand(30, randXCtrl.value); h = rand(30, randYCtrl.value); } else { w = randXCtrl.value; h = randYCtrl.value; } if (linkCtrl.value){w=h;} // Random position const x = rand(startxCtrl.value, canvasW - w); const y = rand(startyCtrl.value, canvasH - h); // Random colour const r = Math.floor(Math.random() * 256); const g = Math.floor(Math.random() * 256); const b = Math.floor(Math.random() * 256); const colour = Colour.createRGBA8({ r, g, b, alpha: 255 }); const fill = FillDescriptor.createSolid(colour); const shape = ShapeRectangle.create(); const rect = { x, y, width: w, height: h }; const def = ShapeNodeDefinition.create(shape, rect, fill, null, null, null); builder.addShapeNode(def); } const cmd = builder.createCommand(true); doc.executeCommand(cmd); console.log('Done: colourful rectangles created!'); } }